package main.com.controller;

import main.com.dao.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author lk
 * @date 2020/6/23 11:28
 * @description: 用户登录
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        UserDao userDao = new UserDao();
        String pwd = userDao.checkUser(username);
        if ("".equals(username)) {
            request.setAttribute("flag", "登录失败");
            request.setAttribute("msg", "用户名为空");
            request.getRequestDispatcher("wel-login.jsp").forward(request, response);
        } else if (pwd == null) {
            request.setAttribute("flag", "登录失败");
            request.setAttribute("msg", "用户不存在");
            request.getRequestDispatcher("wel-login.jsp").forward(request, response);
        } else if (!pwd.equals(password)) {
            request.setAttribute("flag", "登录失败");
            request.setAttribute("msg", "用户名或密码错误");
            request.getRequestDispatcher("wel-login.jsp").forward(request, response);

        } else {
            request.setAttribute("flag", "登录成功");
            request.getSession().setAttribute("username", username);
            request.getRequestDispatcher("wel-login.jsp").forward(request, response);
        }
    }
}
